package com.hnevc.androiduiclassone9;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.File;
import java.io.FileReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Type;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private static final String TAG ="MainActivity" ;

    Button mBtnSh,mBtnBj,mBtnGz;
    TextView mTvName;
    private List<Weather> weathers;



    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

       initView();

        String json1 = "{\"name\":\"zhangsan\",\"age\":\"27\",\"married\":\"true\"}";
        Gson gson = new Gson();
        Person person =  gson.fromJson(json1,Person.class);
        Log.e(TAG, "onCreate: "+person.getName());

        String json2 = "[16,2,26]";
        Type listType = new TypeToken<List<Integer>>(){}.getType();
        List<Integer> ages = gson.fromJson(json2,listType);
        Log.e(TAG, "onCreate: "+ages.get(2) );

        String json3="[" +
                "  {\"temp\":\"20℃/30℃\",\"weather\":\"晴转多云\",\"name\":\"上海\",\"pm\":\"80\",\"wind\":\"1级\"},\n" +
                "  {\"temp\":\"15℃/24℃\",\"weather\":\"晴\",\"name\":\"北京\",\"pm\":\"98\",\"wind\":\"3级\"},\n" +
                "  {\"temp\":\"26℃/32℃\",\"weather\":\"多云\",\"name\":\"广州\",\"pm\":\"30\",\"wind\":\"2级\"}\n" +
                "]\n";
        listType = new TypeToken<List<Weather>>(){}.getType();
        InputStream in = getResources().openRawResource(R.raw.weather2);
        InputStreamReader reader = new InputStreamReader(in);
        weathers = gson.fromJson(reader, listType);
        Log.e(TAG, "onCreate: "+ weathers.get(2).getWeather() );

    }

    private  void initEvent(){
        mBtnBj.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Weather weather = weathers.get(1);
                mTvName.setText(weather.getName());
            }
        });
    }

    private void initView() {
        mBtnBj = findViewById(R.id.btn_bj);
        mBtnSh = findViewById(R.id.btn_sh);
        mBtnGz = findViewById(R.id.btn_gz);
        mTvName = findViewById(R.id.tv_name);
    }
}
